package com.fenxiangle.yueding.common.widget.popupwindow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.suozhang.framework.widget.PasswordEditText;

public class PayPopupWindow extends PopupWindow {
    private LayoutInflater inflater = null;
    private Activity mActivity;
    public PayPopupWindow(Activity activity) {
        this.mActivity = activity;
        inflater = LayoutInflater.from(mActivity);
    }
    @SuppressLint("WrongConstant")
    public void showMoreWindow(View anchor,String title, PopupWindowListener windowListener) {
        final RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mActivity).inflate(R.layout.pay_popup, null);

        //初始化一个PopupWindow，width和height都是WRAP_CONTENT
        final PopupWindow popupWindow = new PopupWindow(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow的视图内容
        popupWindow.setContentView(layout);

        popupWindow.setFocusable(true);//弹出键盘
        //键盘弹出会遮住输入框
        popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        DisplayMetrics dm = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupWindow.setHeight(popupWindow.getContentView().getMeasuredHeight());

        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = 0.4f;
        mActivity.getWindow().setAttributes(lp);


        PasswordEditText pay_passWord=layout.findViewById(R.id.pay_passWord);
        TextView pay_title=layout.findViewById(R.id.pay_title);
        pay_title.setText(title);
        pay_passWord.setPasswordEditTextListener(new PasswordEditText.PasswordEditTextListener() {
            @Override
            public void password(String passWord) {

                if(!TextUtils.isEmpty(passWord)&&passWord.length()==6){
                    windowListener.payPassWord(passWord);
                    popupWindow.dismiss();
                }

            }
        });
        TextView pay_wangji=layout.findViewById(R.id.pay_wangji);
        pay_wangji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        ImageView imageView=layout.findViewById(R.id.btn_pay_exit);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        //点击空白区域PopupWindow消失，这里必须先设置setBackgroundDrawable，否则点击无反应
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(true);
        //设置PopupWindow动画
        popupWindow.setAnimationStyle(R.style.PopupWindow);
        //设置是否允许PopupWindow的范围超过屏幕范围
        popupWindow.setClippingEnabled(true);
        //设置PopupWindow消失监听
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                lp.alpha = 1f;
                mActivity.getWindow().setAttributes(lp);
            }
        });
        int[] location = new int[2];
        anchor.getLocationOnScreen(location);

        //PopupWindow在targetView下方弹出
        popupWindow.showAtLocation(anchor, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);

    }


    public interface PopupWindowListener{
       void payPassWord(String passEord);
    }
}
